package com.xcw.test.caozuo;

/**
 * @author bhx
 * @Class P4_
 * @date 2022/11/13 2:53
 * @Description TODO
 */

import java.util.Arrays;
import java.util.Scanner;

/**
 * 4、已有一个从小到大排好序的9个元素的数组，今输入一个数要求按原来排序的规律将它插入数组中。
 * (1)本题分值:20分
 * (2）考核时间:36min
 * (3)考核形式:上机实操
 * (4）编程语言:C/C++、Python、Java任选一种语言
 * (5）输入:第一行，原始数列。第二行，需要插入的数字。
 * (6)输出:排序后的数列
 * (7）样例输入: 1 7 8 17 23 24 59 62 101
 * (8）样例输出: 17 8 17 23 24 50 59 62 101
 */
public class P4_ {
    private static Scanner input;

    public static void putInNumber(int[] ints, int number){
        int temp = 0;
        int tempd = 0;
        boolean flag = true;
        for (int i = 1; i < ints.length; i++) {
            if (flag){
                if (number >= ints[i-1]&&number <= ints[i]){
                    temp = ints[i];
                    ints[i] = number;
                    flag = false;
                }
                if (i == ints.length - 1){
                    ints[i] = number;
                }
                continue;
            }
            tempd = ints[i];
            ints[i] = temp;
            temp = tempd;
        }
    }

    public static void main(String[] args) {
        System.out.println("请输入数列：");
        input = new Scanner(System.in);
        String line = input.nextLine();
        System.out.println("请输入要插入的数字：");
        String number = input.nextLine();
        String[] lines = line.split("\\s+");
        int[] intslines = new int[lines.length + 1];
        for (int i = 0; i < lines.length; i++) {
            intslines[i] = Integer.valueOf(lines[i].trim());
        }
        putInNumber(intslines,Integer.valueOf(number));
        System.out.println(Arrays.toString(intslines));

    }
}
